package com.jia.blog.controller;


import com.jia.blog.common.lang.R;
import com.jia.blog.entity.Blog;
import com.jia.blog.service.BlogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiajia
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "博客文章管理")
public class BlogController {

    @Autowired
    BlogService blogService;

    @GetMapping("/list")
    public R list(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        Map map = blogService.getList(pageNum,pageSize);
        return R.ok().data(map);
    }

    @GetMapping("/getListByCateId/{cateId}")
    public R getListByCateId(@PathVariable Long cateId){
        List<Blog> list = blogService.getListByCateId(cateId);
        return R.ok().data("data",list);
    }
    @GetMapping("/getListByUserId/{userId}")
    public R getListByUserId(@PathVariable Long userId){
        List<Blog> list = blogService.getListByUserId(userId);
        return R.ok().data("data",list);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        Blog blog = blogService.getById(id);
        Assert.notNull(blog,"该文章已被删除");
        return R.ok().data("data",blog);
    }

    @PostMapping("/save")
    public R save(@Validated @RequestBody Blog blog){
        blogService.save(blog);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@Validated @RequestBody Blog blog){
        blogService.updateById(blog);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id){
        blogService.removeById(id);
        return R.ok();
    }


}
